#include "common/config.hpp"

#include <fstream>
#include <sstream>

std::string Config::DB_HOST = "localhost";
std::string Config::DB_USER = "diabio";
std::string Config::DB_PASSWORD = "diabio";
std::string Config::DB_NAME = "web_cloud_storage_service";
int Config::SERVER_PORT = 8080;
std::string Config::UPLOAD_PATH = "./static/uploads/";


bool Config::load(const std::string& configFile) {
    std::ifstream file(configFile);
    if (!file.is_open()) {
        return false;
    }

    std::string line;
    while (std::getline(file, line)) {
        if (line.empty() || line[0] == '#') continue;

        size_t pos = line.find("=");
        if (pos != std::string::npos) {
            std::string key = line.substr(0, pos);
            std::string value = line.substr(pos + 1);

            if (key == "db.host") DB_HOST = value;
            else if (key == "db.user") DB_USER = value;
            else if (key == "db.password") DB_PASSWORD = value;
            else if (key == "db.name") DB_NAME = value;
            else if (key == "server.port") SERVER_PORT = std::stoi(value);
            else if (key == "upload.path") UPLOAD_PATH = value;
        }
    }
    return true;
}
